package com.yuanfeng.userms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.config.YFConfig;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.userms.entity.UserLogActionEntity;
import com.yuanfeng.userms.mapper.UserLogActionMapper;
import com.yuanfeng.userms.service.UserLogActionService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Map;

/**
 *
 */
@Service
public class UserLogActionServiceImpl extends ServiceImpl<UserLogActionMapper, UserLogActionEntity>
    implements UserLogActionService{

    @Override
    public ResponseResult queryUserOperateLog(Map<String, Object> paraMap) {
        String token = (String) paraMap.get("token");
        String userInfoStr = RedisUtil.get(YFConfig.YF_PLATFORM_TOKEN_USER + token);

        if (null == userInfoStr || userInfoStr.equals("")) {
            return ResponseResult.fail(BizCodeEnume.INSUFFICIENT_PERMISSIONS);
        }

        String userName = (String) paraMap.get("userName");
        String userNickName = (String) paraMap.get("userNickName");
        String userMobile = (String) paraMap.get("userMobile");

        paraMap.put("userName", userName);
        paraMap.put("userNickName", userNickName);
        paraMap.put("userMobile", userMobile);
        //分页查询数据
        IPage<Map<String, Object>> page = this.baseMapper.queryUserOperateLog(new Query<Map<String, Object>>().getPage(paraMap), paraMap);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        SimpleDateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (null != page.getRecords())
            try {
                for (Map<String, Object> map : page.getRecords()) {
                    String dateTime = defaultFormat.format(map.get("log_time"));
                    map.put("log_time",dateTime);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        return ResponseResult.success(PageUtils.getPage(page));
    }
}




